package br.com.poolme.connection;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.StreamConnection;

public abstract class BluetoothConnection implements Runnable {

    protected LocalDevice localDevice;
    protected String playerName;
    public static final int PLAYER_NAME_KEY = 51;
    protected static final UUID RFCOMM_UUID = new UUID(0x7C3434CC);
    protected StreamConnection conn;
    protected static String serverUrl = "btspp://localhost:"
            + RFCOMM_UUID.toString() + ";name=poolme;authorize=false";
    private Thread thread;
    private ConnectionListener listener;

    public DataInputStream getDataInputStream() throws IOException {
        return conn.openDataInputStream();
    }

    public DataOutputStream getDataOutputStream() throws IOException {
        return conn.openDataOutputStream();
    }

    public void start(String playerName, ConnectionListener listener) {
        this.listener = listener;
        this.playerName = playerName;
        thread = new Thread(this);
        thread.start();
    }

    public abstract void stop();

    protected void fireConnectionEstablishedEvent() {
        listener.connectionEstablished();
    }

    protected void fireConnectionErrorEvent(Exception e) {
        listener.connectionError(e);
    }

}